import {compose, withState,} from "recompose"
import {keySeparator} from "./menu-creator"
import {rootPath} from "../../../../shared/init"

const mapping = {
  [`${rootPath}/clothing/append/platform`]: `${rootPath}/clothing/platform-clothing/platform`
}

const menuCompose = compose(
  //pathname = /A/B/C ---> A$B$C
  withState('selectedKeys', 'setSelectedKeys', ({context}) => {
    const {pathname} = context
    const _pathname = mapping[pathname] ? mapping[pathname] : pathname
    return [_pathname.split('/').filter(i => i).join('$')]
  }),
  //pathname = /A/B/C ---> [A,A$B]
  withState('openKeys', 'setOpenKeys', ({context}) => {
    const {pathname} = context
    const _pathname = mapping[pathname] ? mapping[pathname] : pathname
    const keyPath = _pathname.split('/').filter(i => i)
    const openKeys = keyPath.reduce((paths, path) => {
      paths.push(paths.length > 0 ? `${paths[paths.length - 1]}${keySeparator}${path}` : path)
      return paths
    }, [])
    return openKeys
  }),
)

export default menuCompose